package jsonTask;

import com.google.gson.Gson;

import java.util.HashMap;

public class Demo2Gson {
    public static void main(String[] args) {

        String jsonStr="{\"id\":\"1500100001\",\"name\":\"施笑槐\",\"age\":\"22\",\"gender\":\"女\",\"clazz\":\"文科六班\"}";
        System.out.println(jsonStr);
        //Gson解析json字符串
        Gson gson = new Gson();
        //传入两个参数，一个字符串，一个解析的格式，用反射来写
        HashMap<String, String> map = new HashMap<String, String>();
        HashMap hashMap = gson.fromJson(jsonStr, map.getClass());
        System.out.println(hashMap);
        System.out.println(hashMap.get("id"));

        //将json解析成java对象
        Student s = gson.fromJson(jsonStr, Student.class);
        System.out.println(s);

        //将java对象转化为json
        Student student = new Student("1222", "张三", 26, "男", "一班");
        String s1 = gson.toJson(student);
        System.out.println(s1);


    }
}
